<?php

namespace app\common\service\order\coupon_order\coupon;

use app\common\facade\auth\Auth;
use app\common\service\coupon\CouponProductChannelService;
use app\common\service\order\coupon_order\Base;
use app\common\service\order\coupon_order\base\SelfEmployedBase;
use app\common\service\order\coupon_order\CouponOrderChannelInterface;
use app\model\CouponOrder;
use app\model\CouponOrderSid;
use app\model\CouponProductChannel;
use think\Exception;
use think\facade\Log;
use think\Model;

class CouponOrderBuild extends SelfEmployedBase
{

    protected $platformPremiumRate = 0; //平台溢价
    protected $agentPremiumRate = 0; //代理商溢价

    /**
     * @var CouponProductChannel $couponProductChannel
     */
    public $couponProductChannel;


    public function __construct(CouponOrder $couponOrder)
    {
        $config = array();

        $this->couponOrder = $couponOrder;

        $agent_id = $this->couponOrder->agent_id ?? $this->agent_id;

        $premiumRate = $couponOrder->premiumRate ?? [];

        if (!empty($premiumRate)) {

            $config['platform'] = $premiumRate['platformPremiumRat'];

            $config['agent'] = $premiumRate['agentPremiumRate'];

        } else {

            $config = (new CouponProductChannelService())
                ->getPremiumRate($this->couponOrder->quote_id, $agent_id);
        }

        $this->platformPremiumRate = $config['platform']; //没设置溢价百分之10

        $this->agentPremiumRate = $config['agent'];

//        Log::info('平台溢价：' . $this->platformPremiumRate);
//        Log::info('代理溢价：' . $this->agentPremiumRate);
    }



//    public function

    /**
     * 代理商利润
     */
    public function getProfit()
    {
        return $this->pay_amount - $this->settle_price;
    }

    /**
     * 公用获取供应商id
     */
    public function getSupplierId()
    {

        return $this->couponOrder->couponProductChannel->supplier_id;

    }


    /**
     * 平台利润
     */
    public function getPlatformProfit()
    {
        return bcsub($this->settle_price, $this->supplier_price, 0);

    }

    /**
     * 代理商看到的成本价
     */
    public function getSettlePrice()
    {

        $handlingFee = bcmul($this->supplier_price, 1.006, 1);

        $handlingFee = $handlingFee ?? 1;//小于0.01 就0.01 的手续费

        $handlingFee = round($handlingFee);

        $rate = bcdiv($this->platformPremiumRate, 100, 3);

        $priceRate = bcadd(1, $rate, 3);

        $temp = bcmul($handlingFee, $priceRate, 1);

        return round($temp);


        //一下是没有千分之六的
        $rate = bcdiv($this->platformPremiumRate, 100, 3);
        Log::info('获取到的平台比例11:' . $this->platformPremiumRate);
        $priceRate = bcadd(1, $rate, 3);

        $temp = bcmul($this->supplier_price, $priceRate, 1);

        return round($temp);

    }

    /**
     * 公共send
     *
     * 供应商结算金额  订单金额-代理商利润-平台利润
     */
    public function getSupplierPrice()
    {
        if ($this->couponProductChannel) {

            return $this->couponProductChannel->cost_price;

        }

        return $this->couponOrder->couponProductChannel->cost_price;
    }

    /**
     * 原价
     * @return int
     */
    public function getOriginAmount()
    {
        return $this->couponOrder->couponProductChannel->market_price;
    }

    /**
     * 支付金额
     * @return float|int
     */
    public function getPayAmount()
    {
        $rate = bcdiv($this->agentPremiumRate, 100, 3);
//        Log::info($this->agentPremiumRate);
        $priceRate = bcadd($rate, 1, 3);

        $temp = bcmul($this->settle_price, $priceRate, 1);
//        Log::info($this->settle_price);
        return round($temp);
    }

    public function getAmount()
    {
        return $this->pay_amount;
    }


    /**
     * 权益商品
     * @return int
     */
    function getServType()
    {
        return PRODUCT_COUPON_SERV;
    }

    /**
     * 小类型
     * @return int
     */
    public function getType()
    {
        return CouponOrder::TYPE_COUPON_SELF_EMPLOYED;
    }

    public function getProductName()
    {
        $product_name = $this->couponOrder->couponProductChannel->name;

        if (!$product_name) {
            throw new Exception('商品名称不能为空');
        }

        return $product_name;
    }

    public function getProductImage()
    {
        return $this->couponOrder->couponProductChannel->image;
    }

    /**
     * 手机
     */
    public function getMobile()
    {
        $params = $this->couponOrder->params;

        return $params['mobile'] ?? null;
    }


    /**
     * 账号
     */
    public function getAccount()
    {
        $params = $this->couponOrder->params;

        return $params['account'] ?? $this->mobile;
    }

    /**
     * sid
     */
    public function getSid()
    {
        return (new CouponOrderSid())->generateNew(PRODUCT_COUPON_SERV);
    }


    public function getQuoteId()
    {
        return $this->couponOrder->quote_id;
    }

    /**
     * @param CouponProductChannel $couponProductChannel
     */
    public function setCouponProductChannel(CouponProductChannel $couponProductChannel): void
    {
        $this->couponProductChannel = $couponProductChannel;
    }


    public function getCreateOrderFrom()
    {
        $app_from = input('app_from');

        do {
            if (empty($app_from)) {
                break;
            }

            return $app_from;

        } while (false);


        return CouponOrderSid::FROM_H5;
    }

    public function getBrandId()
    {
        return $this->couponOrder->brand_id;
    }


}